//
//  FANStatusListViewModel.m
//  Weibo - OC
//
//  Created by user on 16/4/21.
//  Copyright © 2016年 user. All rights reserved.
//

#import "FANStatusListViewModel.h"
#import "NetworkTools.h"
#import "FANUserAccountModel.h"
#import "FANStatusViewModel.h"
#import "FANStatusPicInfo.h"
#import "FANStatus.h"
#import <SDWebImage/SDWebImageManager.h>

@implementation FANStatusListViewModel

- (void)loadData:(BOOL)isPull and:(void (^)(BOOL))finished{
    
    NSString *urlString = @"https://api.weibo.com/2/statuses/friends_timeline.json";
    
    long long max_id = 0;
    
    if (isPull) {
        
        max_id = [self.statusViewModelArray lastObject].status.id;
        
        if (max_id > 0) {
            
            max_id--;
        }
        
    }
    
    NSDictionary *params = @{
                             
                             @"access_token": [FANUserAccountModel sharedAccountModel].account.access_token,
                             @"max_id" : [NSString stringWithFormat:@"%lld",max_id]
                             
                             };
    
    [[NetworkTools sharedTools] request:FANRequestMethodGet andUrlString:urlString andParameter:params andBlock:^(id responseObject, NSError *error) {
        
        if (error != nil) {
            
            NSLog(@"%@",error);
            
            finished(false);
            
            return;
        }
        
        if (![responseObject isKindOfClass:[NSDictionary class]]) {
            
            finished(false);
            
            return;
            
        }
        
        if (![responseObject[@"statuses"] isKindOfClass:[NSArray class]]) {
            
            finished(false);
            
            return;
        }
        
        NSArray *dictArray = responseObject[@"statuses"];
        
        NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:dictArray.count];
        
        for (NSDictionary *dict in dictArray) {
            
            FANStatus *status = [FANStatus modelWithDict:dict];
            
            FANStatusViewModel *statusViewModel = [[FANStatusViewModel alloc] init];
            
            statusViewModel.status = status;
            
            [mutableArray addObject:statusViewModel];
        }
        
//        self.statusViewModelArray = mutableArray;
        
        if (self.statusViewModelArray == nil) {
            
            self.statusViewModelArray = [NSMutableArray array];
        }
        
        if (isPull) {
            
            for (FANStatusViewModel *statusViewModel in mutableArray) {
                
                [self.statusViewModelArray addObject:statusViewModel];
            }
        }else{
            
            for (FANStatusViewModel *statusViewModel in self.statusViewModelArray) {
                
                [mutableArray addObject:statusViewModel];
            }
            
            self.statusViewModelArray = mutableArray;
        }
        
        
        [self setCachePictureImage:mutableArray and:finished];
        
    }];
}

- (void)setCachePictureImage:(NSArray *)array and:(void (^)(BOOL))finished{
    
    dispatch_group_t group = dispatch_group_create();
    
    for (FANStatusViewModel *statusViewModel in array) {
        
        NSArray *originalPic_urls = statusViewModel.status.pic_urls;
        
        NSArray *retweetPic_urls = statusViewModel.status.retweeted_status.pic_urls;
        
        NSArray<FANStatusPicInfo *> *pic_urls = (originalPic_urls == nil || originalPic_urls.count == 0) ? retweetPic_urls : originalPic_urls;
        
        if (pic_urls.count == 1) {
            
            NSString *imageString = pic_urls.lastObject.thumbnail_pic;
            
            dispatch_group_enter(group);
            
            [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:imageString] options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
               
                [statusViewModel SinglePictureUpdate];
                
                dispatch_group_leave(group);
            }];
            
        }
        
    }
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        
        finished(true);
    });

}

@end
